<?php
//
// This page is a modifed version of the index.php that comes pre-packaged with the Netstats mod for Minecraft created by Valrix (http://forums.bukkit.org/members/valrix.614/).
// All this page does is print out a table listing all the stats you're currently tracking for players using Netstats.
//
// Installation:
// Open config.php and set the paths to your player databases and config.txt (the Netstat mod config file)
// Login to your Minecraft server atleast once so a <Player>.stats file can be created
//
// How to use this page:
// Simply include it on a page on your existing Minecraft website or link to it directly.
//
// Created by: Valrix (http://forums.bukkit.org/members/valrix.614/)
// Version: 1.0
// Modified by: FiZi (http://forums.bukkit.org/members/fizi.21094/)
// Date: March 14th, 2011
//

@include 'config.php'; // Include our config file
date_default_timezone_set(date_default_timezone_get());

if ($netstatConfig == "" || $playerFolder == "")  { // Simple error checking to make sure the paths are defined

	print '<p> <b>Error:</b> Your configuration file is not set properly. Please make sure "<b>$netstatConfig</b>" and  "<b>$playerFolder</b>" are set.';
	exit;
	
}

if (!fopen($netstatConfig, "r")) { // Check to make sure the Netstat configuration file can be read

	print '<p> <b>Error:</b> Your Netstat configuration file cannot be read. Please check the permissions and reload this page. </p>';
	exit;
	
}

foreach (glob($playerFolder."/*.stats") as $player) { // Check to make sure the player stats files can be read
	
	if (!fopen($player, "r")) {
	
		print '<p> <b>Error:</b> One or all of your player stats files cannot be read. Please check the permissions and reload this page. </p>';
		exit;
	
	}

}


// By default we're going to assume we aren't tracking anything.
// These values will be updated by reading the config.txt ($netstatConfig).
$trackIP = false; // Tracking IPs?
$trackBroken = false; // Tracking blocks broken?
$trackPlaced = false; // Tracking blocks placed?
$trackDeaths = false; // Tracking player deaths?

// Now we're going to override our defaults we set above with what's actually in
// the config.txt ($netstatConfig) file.
$handle = fopen($netstatConfig, "r"); // Open the config file for reading
while ($data = fgets($handle)) {
	if (!(substr($data, 0, 1) == "#")) { // Check to see if the first character on a line is a #. If so ignore the line it's a comment.
	    list($key, $value) = explode("=", $data); // Read through each line of the config file splitting values using the delimiter '=' and creating a list
	    $value = trim($value); // Remove any trailing white space on the values to the right of the '=' delimiter
	    switch($key) { // Check each keys value and set it accordingly TRUE/FALSE based on whether or not we're tracking that information
	    	case "trackIP": // Tracking IPs?
	    		$trackIP = ($value == 'true') ? TRUE : FALSE;
	    		break;
	    	case "trackBroken": // Tracking blocks broken?
	    		$trackBroken = ($value == 'true') ? TRUE : FALSE;
	    		break;
	    	case "trackPlaced": // Tracking blocks placed?
	    		$trackPlaced = ($value == 'true') ? TRUE : FALSE;
	    		break;
	    	case "trackDeaths": // Tracking player deaths?
	    		$trackDeaths = ($value == 'true') ? TRUE : FALSE;
	    		break;
	    	default: // If nothing matches break
	    		break;
	    }
	}
}
fclose($handle);


$playerStatFileCount = glob($playerFolder."/*.stats"); //Check to see if there are any player stats files

if (count($playerStatFileCount) == 0 && $printTableIfNoStats == 0) { // If no player stats files are found and $printTableIfNoStats is set to 0 we won't print out the table
	
	print '<p>No one has logged into your server yet</p>';
	
}
else { 
	// Print out our HTML Table
	print '<table border="0" cellpadding="2" cellspacing="5">'; // Change the border, cellpadding and cellspacing to alter the look and feel of your table
	print '<tr><th>Name</th>';
	if ($trackBroken) { // If we're tracking broken blocks create a column for it
		print '<th>Blocks Broken</th>';
	}
	if ($trackPlaced) { // If we're tracking blocks placed create a column for it
		print '<th>Blocks Placed</th>';
	}
	if ($trackDeaths) { // If we're tracking deaths create a column for it
		print '<th>Deaths</th>';
	}
	print '<th>Last Login</th>'; // Last login column
	print '<th>Last Seen</th>'; // Last seen column
	print '<th>Play Time</th>'; // Play time column
	if ($trackIP) { // If we're tracking player IP's create a column for it
		print '<th>Player IP</th>';
	}
	print '</tr>';
	
	// Now we can grab the data for each player and output it. We'll read line per line for each user
	foreach (glob($playerFolder."/*.stats") as $player) { // Search for each .stats file in our player folder ($playerFolder). This will prevent other files from accidentally being read.
		// Grab all player data and shove into an array as key => value
		$playerData = array();
		$handle = fopen($player, "r");
		while ($data = fgets($handle)) {
			if (!(substr($data, 0, 1) == "#")) { //Remove any lines starting with a '#' because they're comments
				list ($key, $value) = explode("=", $data); // Read through each line of the config file splitting values using the delimiter '=' and creating a list
				$value = trim($value);
				switch($key) {
					case "online":
						$playerData[$key] = ($value == 'true') ? TRUE : FALSE;
						break;
					case "broken":
						$playerData[$key] = (int)$value;
						break;
					case "placed":
						$playerData[$key] = (int)$value;
						break;
					case "deaths":
						$playerData[$key] = (int)$value;
						break;
					case "enter":
						$playerData[$key] = (int)$value;
						break;
					case "seen":
						$playerData[$key] = (int)$value;
						break;
					case "total":
						$playerData[$key] = (int)$value;
						break;
					case "IP":
						$playerData[$key] = $value;
						break;
					default:
						break;
				}
			}
		}
		fclose($handle);
		// Now output it depending on what keys are there
		$name = explode(".", basename($player)); // Get the player name by looking at the file name of the .stats file and drop the .stats
		$total = $playerData['total'];
		// Total playtime
		$total = $total/1000;
		$sec = $total%60;
		$total = $total/60;
		$min = $total%60;
		$total = $total/60;
		$hrs = $total%24;
		$total = $total/24;
		$day = $total%24;
		$time = ($day) ? $day." days " : '';
		$time .= ($hrs) ? $hrs." hours " : '';
		$time .= ($min) ? $min." mins " : '';
		$time .= $sec." secs";
		// Time they've been gone
		$ago = ((time()*1000)-$playerData['seen'])/1000;
		$asec = $ago%60;
		$ago = $ago/60;
		$amin = $ago%60;
		$ago = $ago/60;
		$ahrs = $ago%24;
		$ago = $ago/24;
		$aday = $ago%24;
		$atime = ($aday) ? $aday." days " : '';
		$atime .= ($ahrs) ? $ahrs." hours " : '';
		$atime .= ($amin) ? $amin." mins " : '';
		$atime .= ($asec) ? $asec." secs " : '';
		$atime .= " ago";
		$atime = ($playerData['online']) ? "Currently Online" : $atime;
		print '<tr>';
		if ($playerData['online']) { // Check if our player is online
			print '<td class="onlinePlayer">'.$name[0].'</td>'; // Green if they are
		} else {
			print '<td class="offlinePlayer">'.$name[0].'</td>'; // Red if they aren't
		}
		if ($trackBroken) { // Broken blocks
			print '<td>'.$playerData['broken'].'</td>';
		}
		if ($trackPlaced) { // Blocks placed
			print '<td>'.$playerData['placed'].'</td>';
		}
		if ($trackDeaths) { // Total deaths
			print '<td>'.$playerData['deaths'].'</td>';
		}
		print '<td>'.date("D, M d g:i A", $playerData['enter']/1000).'</td>'; //
		print '<td>'.$atime.'</td>';
		print '<td>'.$time.'</td>';
		if ($trackIP) { // Players IP address
			print '<td>'.$playerData['IP'].'</td>';
		}
		print '</tr>';
		print '</table>';
	}

}

?>